local skill = fk.CreateSkill {
  name = "#ling__jade_seal_skill",
  attached_equip = "ling__jade_seal",
}

Fk:loadTranslationTable{
  ["#ling__jade_seal_skill"] = "玉玺",
  ["#ling__jade_seal-choose"] = "玉玺：你可以令一名角色交给你一张牌，本阶段结束时交给其一张牌",
  ["#ling__jade_seal-give"] = "玉玺：请交给 %dest 一张牌",
}

local spec = {
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function (p)
      return not p:isNude()
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = skill.name,
      prompt = "#ling__jade_seal-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:addTableMark(player, "ling__jade_seal-phase", to.id)
    local card = room:askToCards(to, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = skill.name,
      prompt = "#ling__jade_seal-give::"..player.id,
      cancelable = false,
    })
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonGive, skill.name, nil, false, to)
  end,
}

skill:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skill.name) and player.phase == Player.Play and
      table.find(player.room:getOtherPlayers(player, false), function (p)
        return not p:isNude()
      end)
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

skill:addEffect(fk.CardUseFinished, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skill.name) and
      data.card.name == skill.attached_equip and
      table.find(player.room:getOtherPlayers(player, false), function (p)
        return not p:isNude()
      end)
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

skill:addEffect(fk.EventPhaseEnd, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skill.name) and not player:isNude() and
      table.find(player.room:getOtherPlayers(player, false), function (p)
        return table.contains(player:getTableMark("ling__jade_seal-phase"), p.id)
      end)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local targets = table.map(player:getTableMark("ling__jade_seal-phase"), Util.Id2PlayerMapper)
    room:sortByAction(targets)
    for _, p in ipairs(targets) do
      if player:isNude() or player.dead then return end
      if not p.dead then
        local card = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = skill.name,
          prompt = "#ling__jade_seal-give::"..p.id,
          cancelable = false,
        })
        room:moveCardTo(card, Card.PlayerHand, p, fk.ReasonGive, skill.name, nil, false, player)
      end
    end
  end,
})

return skill
